$(function () {



  $(document).on('click', '.doct-detail-header-item > span[data-id]', handleDepClick);
  $(document).on('click', '.allow-reserve', handleReserveClick);
  // $(document).on('click', '.dep-detail-attention.no-attention', handleAttentionClick);
  $(document).on('click', '.dep-detail-attention.has-attention', handleHasAttentionClick);
  $(document).on('click', '.doc-detail-more', handleHosMore);

  function handleDepClick() {
    $('.doct-detail-header-item > span.active').removeClass('active');
    $(this).addClass('active');
    setSchemaList($(this).data('id'));
  }

  function setSchemaList(deptid) {
    var _schemaData = schemaData.filter(function (v) {
      return v.deptid == deptid;
    });
    var morHtmlData = '<div  class="float-left" style="width: 12%; border-right: 1px solid #DFDFDF; line-height: 68px;">上午</div>';
    for (var i = 0; i < date.length; i++) {
      var reserve = (_schemaData.filter(function (v) {
        return (v.deptid == deptid) && (v.scheduledate === date[i].format) && (v.timerange === '1');
      })[0] || {});
      var reservenum = reserve.reservenum;
      morHtmlData += '<div class="float-left" style="width: 11%;">';
      if (reserve.state === '1') {
        morHtmlData += '<div class="stop-reserve">停诊</div>';
      } else if (reservenum == '0') {
        morHtmlData += '<div class="full-reserve">已满</div>';
      } else if (+reservenum > 0) {
        morHtmlData += '<div class="allow-reserve"  data-href="/reservation/' + reserve.scheduleid + '?scheduledate=' + reserve.scheduledate + '&timerange=' + reserve.timerange + '&regfee=' + reserve.regfee  + '">预约(余号' + reservenum + ')</div>';
      }
      morHtmlData += '</div>';
    }
    var noonHtmlData = '';
    var noonHtmlData = '<div  class="float-left" style="width: 12%; border-right: 1px solid #DFDFDF; line-height: 68px;">下午</div>';
    for (var i = 0; i < date.length; i++) {
      var reserve = (_schemaData.filter(function (v) {
        return (v.deptid == deptid) && (v.scheduledate === date[i].format) && (v.timerange === '2');
      })[0] || {});
      var reservenum = reserve.reservenum;
      noonHtmlData += '<div class="float-left" style="width: 11%;">';
      if (reserve.state === '1') {
        noonHtmlData += '<div class="stop-reserve">停诊</div>';
      } else if (reservenum == '0') {
        noonHtmlData += '<div class="full-reserve">已满</div>';
      } else if (+reservenum > 0) {
        noonHtmlData += '<div class="allow-reserve"  data-href="/reservation/' + reserve.scheduleid + '?scheduledate=' + reserve.scheduledate + '&timerange=' + reserve.timerange +  '&regfee=' + reserve.regfee  + '">预约(余号' + reservenum + ')</div>';
      }
      noonHtmlData += '</div>';
    }
    $('.doc-detail-content-mor').html(morHtmlData);
    $('.doc-detail-content-noon').html(noonHtmlData);
  }

  function handleReserveClick() {
    window.location.href = $(this).data('href') +
        '&depname=' +
        encodeURIComponent($('.doct-detail-header-item .active').text()) +
        '&hosid=' +
        $('.doct-detail-header-item .active').data('hos') +
        '&docid=' +
        $('.doct-detail-header-item .active').data('doc') +
        '&hosname=' +
        $('.doct-detail-header-item .active').data('hosname');
  }

  function handleAttentionClick() {
    $.post('/api/addFav', {
      hosid: hosData.hosid,
      type: 3,
      favid: docData.docid,
      specificName: docData.docname,
    }).then(function(res) {
      if(res.code === 0) {
        success_prompt(res.data);
        $('.dep-detail-attention.no-attention').removeClass('no-attention')
            .addClass('has-attention').children('span').text('已关注');
      }
    });
  }

  function handleHasAttentionClick() {
    $.post('/api/cancelFav', {
      hosid: hosData.hosid,
      type: 3,
      favid: docData.docid,
      specificName: docData.docname,
    }).then(function(res) {
      if(res.code === 0) {
        success_prompt(res.data);
        $('.dep-detail-attention.has-attention').removeClass('has-attention')
            .addClass('no-attention').children('span').text('关注');
      }
    });
  }

  function handleHosMore() {
    var $this = $(this);
    var $header = $('.doc-detail-header');
    if($this.hasClass('active')) {
      $this.removeClass('active');
      $this.children('span').first().text('查看更多医院');
      $header.each(function(i, v) {
        if(i > 2) {
          $(v).addClass('d-none');
        }
      });
    } else {
      $this.addClass('active');
      $this.children('span').first().text('收起');
      $header.removeClass('d-none');
    }
    $this.parent('.doc-detail-header').removeClass('d-none');
  }

  var doctorInfo = new Vue({
    el:"#doctorInfo",
    data:{
      doctorPage:{},//医生信息
      hospitalId:"",
      tdId:"",
      hospitalURL:"",//医院跳转路径
      doctorscheduleList:[],//医生排版表数据
      tDepartmentsDoctorList:[],//获取回来的同科室医生数据
      sreachDate:"",//搜索框数据
      payDoctor:"关注",
      token:localStorage.getItem('token')
    },
    methods:{
      getDoctorPage:function () {
        var doctorId = 1;
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
          var pair = vars[i].split("=");
          if(pair[0] == "doctorId"){doctorId = pair[1];}
        }
        var that = this;
        axios.get("../api/doctor/pageDoctor?doctorId="+doctorId).then(function(response){
          if (response.data.code!="0"){
            alert(response.data.msg)
            return;
          }
          that.doctorPage = response.data.data;
          that.hospitalURL = "hospital.html?hospitalId="+response.data.data.hospitalId;
          that.hospitalId = response.data.data.hospitalId;
          that.tdId = response.data.data.tdId;
          console.log(response.data.data)
          console.log(that.doctorPage)
          that.gettDepartmentsDoctor();
        }).catch(function(err){
          console.log(err);
        });
      },
      getDoctorschedule:function () {
        var doctorId = 1;
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
          var pair = vars[i].split("=");
          if(pair[0] == "doctorId"){doctorId = pair[1];}
        }
        var that = this;
        axios.get("../api/schedule/getDoctorschedule?doctorId="+doctorId).then(function(response){
          if (response.data.code!="0"){
            alert(response.data.msg)
            return;
          }
          that.doctorscheduleList = response.data.data;
          console.log(response.data.data)
        }).catch(function(err){
          console.log(err);
        });
      },
      gettDepartmentsDoctor:function () {
        var doctorId = 1;
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
          var pair = vars[i].split("=");
          if(pair[0] == "doctorId"){doctorId = pair[1];}
        }
        var that = this;
        axios.get("../api/doctor/gettDepartmentsDoctor?doctorId="+doctorId+"&hospitalId="+that.hospitalId+"&tDepartmentsId="+that.tdId).then(function(response){
          if (response.data.code!="0"){
            alert(response.data.msg)
            return;
          }
          that.tDepartmentsDoctorList = response.data.data;
          console.log(response.data.data)
        }).catch(function(err){
          console.log(err);
        });
      },
      toDoctor:function (data) {
        location.href = "doctor.html?doctorId="+data;
      },
      toTdepartment:function () {
        var that = this;
        if (that.sreachDate == null || that.sreachDate == "" || that.sreachDate == undefined){
          alert("搜索数据不能为空")
          return false;
        }
        axios.get("../api/tdepartments/getTdepartmentsByCondition?hospitalId="+that.hospitalId+"&tdepartmentsName="+that.sreachDate).then(function(response){
          //alert(response.data.code)
          if (response.data.code!="0"){
            alert("没有相关科室")
            return;
          }
          console.log(response.data.data)
          location.href="department.html?hospitalId="+that.hospitalId+"&tdepartmentsId="+response.data.data;
        }).catch(function(err){
          console.log(err);
        });
      },
      toDoctor02:function () {
        var that = this;
        if (that.sreachDate == null || that.sreachDate == "" || that.sreachDate == undefined){
          alert("搜索数据不能为空")
          return false;
        }

        axios.get("../api/doctor/getDoctorByCondition?hospitalId="+that.hospitalId+"&doctorName="+that.sreachDate).then(function(response){
          if (response.data.code!="0"){
            alert("没有相关医生")
            return;
          }
          console.log(response.data.data)
          location.href="doctor.html?doctorId="+response.data.data;
        }).catch(function(err){
          console.log(err);
        });
      },
      toCheckOrder:function (date,doctorId,dayTime) {
        location.href="payment.html?date="+date+"&doctorId="+doctorId+"&dayTime="+dayTime;
      },
      payDoctorFun:function () {
        var that = this;
        var doctorId = 1;
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
          var pair = vars[i].split("=");
          if(pair[0] == "doctorId"){doctorId = pair[1];}
        }
        axios.get("../api/doctor/payDoctor?doctorId="+doctorId,
            {headers:{'token':localStorage.getItem("token")}}).then(function(response){
          if (response.data.code != "0"){
            alert(response.data.msg);
            return;
          }
          if(response.data.data.msg=="已关注过该医生"){
            console.log(response)
            alert(response.data.data.msg)
            that.payDoctor = "关注数:"+response.data.data.data;
            return;
          }
          that.payDoctor = "关注数:"+response.data.data;
        }).catch(function(err){
          console.log(err);
        });
      }
    },
    created(){
      this.getDoctorPage();
      this.getDoctorschedule();
    }
  })

  var userOrLogin = new Vue({
    el:"#userOrLogin",
    data:{
      token:localStorage.getItem('token')
    }
  })
  // var detailContent = new Vue({
  //   el:"#detail_content",
  //   data:{
  //     doctorscheduleList:[]
  //   },
  //   methods:{
  //     getDoctorschedule:function () {
  //       var that = this;
  //       axios.get("../api/schedule/getDoctorschedule?doctorId=1").then(function(response){
  //         if (response.data.code!="0"){
  //           alert(response.data.msg)
  //           return;
  //         }
  //         that.doctorscheduleList = response.data.data;
  //         console.log(response.data.data)
  //       }).catch(function(err){
  //         console.log(err);
  //       });
  //     }
  //   },
  //   created() {
  //     this.getDoctorschedule();
  //   }
  // })

  // var docDetailHeader = new Vue({
  //   el:"#docDetailHeader",
  //   data:{
  //     hospitalName:localStorage.getItem("hospitalName"),
  //     tdepartmentsName:localStorage.getItem("tdepartmentsName")
  //   }
  // })
  //alert(localStorage.getItem("hospitalName"))

  var getTokenVue = new Vue({
    el:"#getToken",
    data:{
      token:localStorage.getItem("token")
    }
    ,methods:{

    }
  })
});